// Datastore.h : Declaration of the CDatastore

#pragma once
#include "resource.h"       // main symbols

#include "TSSrv_i.h"

#include "DBHelper.h"
#include "FieldDefinitions.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif



// CDatastore

class ATL_NO_VTABLE CDatastore :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CDatastore, &CLSID_Datastore>,
	public ISupportErrorInfo, 
	public IDispatchImpl<IDatastore, &IID_IDatastore, &LIBID_TSSrv, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
    friend class CDatastores;
public:
	CDatastore()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_DATASTORE)


BEGIN_COM_MAP(CDatastore)
	COM_INTERFACE_ENTRY(IDatastore)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
		if (m_pDBHelper != NULL)
		{
			delete m_pDBHelper;
		}
	}

public:
	/// <summary>Eexcute query base on gived Query.</summary>
    STDMETHOD(Execute)(IDatastoreItems** ppDatastoreItems);
	/// <summary>return current product name.</summary>
    STDMETHOD(get_ProductName)(BSTR* pVal);
	/// <summary>return Query.</summary>
    STDMETHOD(get_Query)(IQuery** pVal);
	/// <summary>set query.</summary>
    STDMETHOD(put_Query)(IQuery* newVal);
	/// <summary>get DatastoreItem by item type and item id.</summary>
	STDMETHOD(GetDatastoreItem)(TsDatastoreItemTypeEnum DatastoreItemType, INT Id, IDatastoreItem** ppDatastoreItem);
	/// <summary>add new DatastoreItem to database.</summary>
	STDMETHOD(AddDatastoreItem)(TsDatastoreItemTypeEnum DatastoreItemType, IDatastoreItem** NewItem);
	/// <summary>return Root Node.</summary>
	STDMETHOD(get_RootNode)(INode** pVal);

	CDBHelper * m_pDBHelper;

private:
    IQuery * m_pQuery;
	CComObject<CFieldDefinitions> m_FieldDefinitions;
public:
	STDMETHOD(get_FieldDefinitions)(IFieldDefinitions** pVal);
};

OBJECT_ENTRY_AUTO(__uuidof(Datastore), CDatastore)
